import authService from './auth.js';

class CourseService {
    async getAllCourses() {
        try {
            const response = await fetch('/api/courses');
            return await response.json();
        } catch (error) {
            console.error('Failed to obtain course list:', error);
            throw error;
        }
    }

    async getCourseById(courseId) {
        try {
            const response = await fetch(`/api/courses/${courseId}`);
            return await response.json();
        } catch (error) {
            console.error('Failed to obtain course details:', error);
            throw error;
        }
    }

    async createCourse(courseData) {
        if (!authService.hasRole('teacher') && !authService.hasRole('admin')) {
            throw new Error('No permission to create courses');
        }

        try {
            const response = await fetch('/api/courses', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(courseData)
            });
            return await response.json();
        } catch (error) {
            console.error('Course creation failed:', error);
            throw error;
        }
    }

    async updateCourse(courseId, courseData) {
        if (!authService.hasRole('teacher') && !authService.hasRole('admin')) {
            throw new Error('No permission to update courses');
        }

        try {
            const response = await fetch(`/api/courses/${courseId}`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(courseData)
            });
            return await response.json();
        } catch (error) {
            console.error('Failed to update course:', error);
            throw error;
        }
    }

    async enrollCourse(courseId) {
        if (!authService.hasRole('student')) {
            throw new Error('Only students can register for courses');
        }

        try {
            const response = await fetch(`/api/courses/${courseId}/enroll`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            });
            return await response.json();
        } catch (error) {
            console.error('Course registration failed:', error);
            throw error;
        }
    }

    async getEnrolledCourses() {
        if (!authService.hasRole('student')) {
            throw new Error('Only students can view registered courses');
        }

        try {
            const response = await fetch('/api/courses/enrolled');
            return await response.json();
        } catch (error) {
            console.error('Failed to retrieve registered courses:', error);
            throw error;
        }
    }

    async submitAssignment(assignmentId, content) {
        if (!authService.hasRole('student')) {
            throw new Error('Only students are allowed to submit assignments');
        }

        try {
            const response = await fetch(`/api/assignments/${assignmentId}/submit`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ content })
            });
            return await response.json();
        } catch (error) {
            console.error('Homework submission failed:', error);
            throw error;
        }
    }

    async getCourseStatistics(courseId) {
        if (!authService.hasRole('teacher') && !authService.hasRole('admin')) {
            throw new Error('No permission to view course statistics information');
        }

        try {
            const response = await fetch(`/api/courses/${courseId}/statistics`);
            return await response.json();
        } catch (error) {
            console.error('Failed to obtain course statistics information:', error);
            throw error;
        }
    }
}

const courseService = new CourseService();
export default courseService;
